package com.example.comment.controller;

import com.example.comment.service.CommentRepayService;
import com.example.common.util.R;
import com.example.model.comment.dto.CommentRepayDto;
import com.example.model.comment.dto.CommentRepayLikeDto;
import com.example.model.comment.dto.CommentRepaySaveDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/")
@Tag(name = "回复管理")
public class CommentRepayController {
    @Autowired
    private CommentRepayService commentRepayService;

    @PostMapping(value = "/commentRepay/getLoadCommentRepay")
    @Operation(summary = "查询更多回复内容") // bug
    public R getLoadCommentRepay(@RequestBody CommentRepayDto commentRepayDto) {
        Object object = commentRepayService.getLoadCommentRepay(commentRepayDto);
        if (object != null) {
            return R.ok("查询更多回复内容成功");
        } else {
            return R.error("查询更多回复内容失败");
        }
    }

    @PostMapping(value = "/commentRepay/addCommentRepay")
    @Operation(summary = "保存回复")
    public R addCommentRepay(@RequestBody CommentRepaySaveDto commentRepaySaveDto) {
        boolean flag = commentRepayService.addCommentRepay(commentRepaySaveDto);
        if (flag) {
            return R.ok("保存回复成功");
        } else {
            return R.error("保存回复失败");
        }
    }

    @DeleteMapping(value = "/commentRepay/deleteCommentRepay/{commentRepayId}")
    @Operation(summary = "删除回复")
    public R deleteCommentRepay(@PathVariable(value = "commentRepayId") String commentRepayId) {
        boolean flag = commentRepayService.deleteCommentRepay(commentRepayId);
        if (flag) {
            return R.ok("删除回复成功");
        } else {
            return R.error("删除回复失败");
        }
    }

    @PostMapping(value = "/commentRepay/addCommentRepayLike")
    @Operation(summary = "点赞回复的评论") // bug
    public R addCommentRepayLike(@RequestBody CommentRepayLikeDto commentRepayLikeDto) {
        Object object = commentRepayService.addCommentRepayLike(commentRepayLikeDto);
        if (commentRepayLikeDto.getOperation() == 0) {
            if (object != null) {
                return R.ok("点赞回复的评论成功");
            } else {
                return R.error("点赞回复的评论失败");
            }
        } else {
            if (object != null) {
                return R.ok("取消点赞回复的评论成功");
            } else {
                return R.error("取消点赞回复的评论失败");
            }
        }
    }
}
